// #include "src/include/LPA.h"
#include "src/include/TPA.h"
#include "src/include/LPAS.h"
#include "src/include/LPA.h"

// 验证多个虚拟机的扩展在两个虚拟机的特例和此前实现一致
int main() {
    Logger::init(LOG_PATH);
    FILE* fp = fopen(FLOW_CSV_PATH, "r");
    if (!fp) {
        perror("无法打开文件");
        return -1;
    }
    int tot = 32;
    while(1) {
        char line[MAX_LINE_SIZE];
        // 测试还有没有line
        if (feof(fp)) break;
        // 跳过标题行（id,src,dst,size,relative_dl,period,priority）
        fgets(line, MAX_LINE_SIZE, fp);
        flows[0].priority = -1;
        // 逐行读取数据
        flow_num = 0;
        while (fgets(line, MAX_LINE_SIZE, fp)) {
            flow_num++;
            flows[flow_num].id           = atoi(strtok(line, ","));
            flows[flow_num].src          = atoi(strtok(NULL, ","));
            flows[flow_num].dst          = atoi(strtok(NULL, ","));
            flows[flow_num].critical     = atoi(strtok(NULL, ","));
            flows[flow_num].size         = atoi(strtok(NULL, ","));
            flows[flow_num].period       = atoi(strtok(NULL, ","));
            flows[flow_num].relative_dl  = atoi(strtok(NULL, ","));
            flows[flow_num].cw           = CBIT_WRITE * flows[flow_num].size * 8;
            flows[flow_num].cr           = CBIT_READ * flows[flow_num].size * 8;
            flows[flow_num].priority     = atoi(strtok(NULL, ","));
            flows[flow_num].is_scheduled = false;
            // printf(
            //     "id: %d, src: %d, dst: %d, critical: %d, period: %d, size: %d, relative_dl: %d, cw: "
            //     "%f, cr: %f\n",
            //     flow_num, flows[flow_num].src, flows[flow_num].dst, flows[flow_num].critical, flows[flow_num].period,
            //     flows[flow_num].size, flows[flow_num].relative_dl, flows[flow_num].cw, flows[flow_num].cr);
            if (flow_num == tot) break;
        }
        static int cnt = 0;
        INFO("%d\n", cnt++);
        get_HP();
        LPAS *prio_assign = new LPAS(flows);
        prio_assign->run();
        // prio_assign->debug_flows();
        // prio_assign->record_flows(LPA_RESULT_CSV_PATH);
        // printf("\nafter separate \n");
        prio_assign->separate_ep_flows();
        prio_assign->debug_flows();
        prio_assign->continuous_record(LPAS_RESULT_CSV_PATH);
        delete prio_assign;
    }
    fclose(fp);
    return 0;
}
